package com.morningstar.qs.api.basic.common.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author dchen2
 *
 */
@Deprecated
public class ConfigEntries {
	
	public ConfigEntries(String el) {
		this.elementName=el;
	}
	
	private Map<String, String> attributes=new HashMap<String, String>();
	private String elementName;
	private String value;
	private Map<String, List<ConfigEntries>> entries = new HashMap<String, List<ConfigEntries>>();

	@Deprecated
	public String toString() {
		StringBuilder sb=new StringBuilder();
		sb.append("en:").append(elementName).append("\t");
		sb.append("value:").append(value).append("\t");
		sb.append("Att:").append(attributes.toString()).append("\r\n");
		sb.append("entries:").append(entries.toString());
		return sb.toString();
	}
	
	@Deprecated
	public void addEntry(ConfigEntries entry) {
		List<ConfigEntries> l = entries.get(entry.getElementName());
		if (l == null) {
			l = new ArrayList<ConfigEntries>();
			entries.put(entry.getElementName(), l);
		}
		l.add(entry);
	}

	@Deprecated
	public String property(String key) {
		if (this.containsKey(key))
			return this.attribute(key);
		for (Map.Entry<String, List<ConfigEntries>> en : entries.entrySet()) {
			for (ConfigEntries ce : en.getValue()) {
				String v = ce.property(key);
				if (v != null)
					return v;
			}
		}
		return null;
	}
	
	@Deprecated
	public String property(ConfigEntries ce,String key) {
		if (ce.containsKey(key))
			return ce.attribute(key);
		for (Map.Entry<String, List<ConfigEntries>> en : ce.getEntries().entrySet()) {
			for(ConfigEntries buce: en.getValue()) {
				String v=property(buce, key);
				if(v!=null) return v;
			}
		}
		return null;
	}
	
	@Deprecated
	public String property(String element, String key) {
		if (entries.containsKey(element)) {
			List<ConfigEntries> ces = entries.get(element);
			for (ConfigEntries ce : ces) {
				String v=property(ce, key);
				if(v!=null) return v;
			}
		}
		return null;
	}
	
	@Deprecated
	public String[] properties(String element,String key) {
		String[] vs=null;
		if (entries.containsKey(element)) {
			List<String> lv=new ArrayList<String>();
			List<ConfigEntries> ces = entries.get(element);
			for (ConfigEntries ce : ces) {
				String v=property(ce, key);
				if(v!=null) lv.add(v);
			}
			vs=new String[lv.size()];
			lv.toArray(vs);
		}
		return vs;
	}

	@Deprecated
	public String attribute(String key) {
		return attributes.get(key);
	}
	
	@Deprecated
	public void addAttribute(String key, String value) {
		attributes.put(key, value);
	}
	@Deprecated
	private boolean containsKey(String key) {
		return attributes.containsKey(key);
	}
	@Deprecated
	public String attribute(String element, String key) {
		if (entries.containsKey(element)) {
			List<ConfigEntries> ces = entries.get(element);
			for (ConfigEntries ce : ces) {
				if (ce.containsKey(key))
					return ce.attribute(key);
			}
		}
		return null;
	}

	@Deprecated
	public Map<String, List<ConfigEntries>> getEntries() {
		return entries;
	}
	@Deprecated
	public void setEntries(Map<String, List<ConfigEntries>> entries) {
		this.entries = entries;
	}

	
	@Deprecated
	public void clear() {
		attributes.clear();
		entries.clear();
	}
	@Deprecated
	public void put(String key, String value) {
		attributes.put(key, value);
		
	}
	@Deprecated
	public Map<String, String> getAttributes() {
		return attributes;
	}
	@Deprecated
	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}
	@Deprecated
	public String getElementName() {
		return elementName;
	}
	@Deprecated
	public void setElementName(String elementName) {
		this.elementName = elementName;
	}
	@Deprecated
	public String getValue() {
		return value;
	}
	@Deprecated
	public void setValue(String value) {
		this.value = value;
	}
	@Deprecated
	public boolean empty() {
		return attributes.size()==0 && entries.size()==0;
	}
	@Deprecated
	

	public String obtainValue(ConfigEntries config, String elementName) {
		if(config.getElementName().equals(elementName)) return config.getValue();
		for(Map.Entry<String, List<ConfigEntries>> en: config.getEntries().entrySet()) {
			for(ConfigEntries sce: en.getValue()) {
				String v=obtainValue(sce, elementName);
				if(v!=null) return v;
			}
		}
		return null;
	}

	@Deprecated
	public List<ConfigEntries> getEntries(String elName) {
		return entries.get(elName);
	}
	

}
